package com.xiao.permission_system.generator;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * @author beth
 * @data 2019-09-12 09:33
 */
public class CodeGenerator {
    /**
     * 生成代码
     */
    private static void generateCode() {
         // 项目路径
         String projectPath = System.getProperty("user.dir");

         // 生成文件人
         String  author = "beth";

         // 数据库连接信息
         String url = "jdbc:mysql://47.106.95.68:3306/permission_system?useUnicode=true&useSSL=false&characterEncoding=utf8";
         String driverName = "com.mysql.cj.jdbc.Driver";
         String username = "root";
         String password = "000000";

         // 父包名
         String parentPackage = "com.xiao.permission_system";

         // 实体类输出包
         String entityPackage = "entity";

        // mapper输出包
        String mapperPackage = "mapper";

         // service输出包
        String servicePackage = "service";

        // serviceImpl输出包
        String serviceImplPackage = "service.impl";

       // controller输出包
       String controllerPackage = "controller";

        // entity输出模版
        String entityTemplate = "templateEngine/entity.java";

        // service输出模版
        String serviceTemplate = "templateEngine/service.java";

        // mapper输出模版
        String mapperTemplate = "templateEngine/mapper.xml";

        // controller输出模版
        String controllerTemplate = "templateEngine/controller.java";

        // 父类实体
        String baseEntity = "cn.xiao.permission_system.common.BaseEntity";

        // 父类控制器
        String baseController = "cn.xiao.permission_system.common.BaseController";

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor(author);
        gc.setOpen(false);
        mpg.setGlobalConfig(gc);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        dsc.setDriverName(driverName);
        dsc.setUsername(username);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //pc.setModuleName(scanner("模块名"));
        pc.setParent(parentPackage);
        pc.setEntity(entityPackage);
        pc.setMapper(mapperPackage);
        pc.setService(servicePackage);
        pc.setServiceImpl(serviceImplPackage);
        pc.setController(controllerPackage);
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 freemarker
        String templatePath = "/templateEngine/mapper.xml.btl";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setEntity(entityTemplate);
        templateConfig.setService(serviceTemplate);
        templateConfig.setController(controllerTemplate);
        templateConfig.setXml(mapperTemplate);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass(baseEntity);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setSuperControllerClass(baseController);
        strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix( "sys_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new BeetlTemplateEngine());
        mpg.execute();
    }

    public static void main(String[] args) {
        generateCode();
    }

}
